package com.docios.cookbook2.ui.adapter;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.docios.cookbook2.ui.homePager.HomeActivity;
import com.docios.cookbook2.ui.otherPager.QueryDetailActivity;
import com.docios.cookbook2.R;
import com.docios.cookbook2.entity.QueryBean;

import java.util.ArrayList;
import java.util.List;

public class ItemHomeFoodAdapter extends RecyclerView.Adapter<ItemHomeFoodAdapter.ViewHolder> {

    //TODO 首页随机分类id查询，下拉刷新分类，加载动画，浏览更新

    List<QueryBean> queryBeans;
    Context context;

    /* 1
     * 构造方法传入上下文和菜谱数据
     * */
    public ItemHomeFoodAdapter(ArrayList<QueryBean> queryBeans, Context context) {
        this.queryBeans = queryBeans;
        this.context = context;
    }

    /* 2
     * 创建ViewHolder实例，传入构造函数，并返回一个ViewHolder
     * */
    @NonNull
    @Override
    public ItemHomeFoodAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_food, parent, false);
        return new ViewHolder(view);
    }

    /* 3
     * 视图绑定
     * */
    @Override
    public void onBindViewHolder(@NonNull ItemHomeFoodAdapter.ViewHolder holder, int position) {
        //存入菜名和封面
        holder.name.setText(queryBeans.get(position).getName());
        //当更新到Glide4.8 时，直接应用 error/preload/fallback 均会报错
        //new一个RequestOptions ，提前将这些配置好
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.food_picture_null)
                .error(R.drawable.food_picture_null)
                .fallback(R.drawable.food_picture_null);
        Glide.with(context)
                .load(queryBeans.get(position).getPic())
                .apply(requestOptions)
                .into(holder.coverImg);

        /*
         * 进入详情页
         * */
        holder.coverImg.setOnClickListener(view -> {
            Intent intent = new Intent(context, QueryDetailActivity.class);
            intent.putExtra("data", queryBeans.get(position));
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return queryBeans.size();
    }

    /* 4
     * ViewHolder内部类
     * */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout itemFood;
        ImageView coverImg;
        TextView name;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            itemFood=itemView.findViewById(R.id.item_food_view);
            coverImg = itemView.findViewById(R.id.cover_img);
            name = itemView.findViewById(R.id.name);
        }
    }
}
